//
//  GetRssDataFunc.m
//  eyeinfo
//
//  Created by kingsoft on 11-1-11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GetRssDataLogical.h"
#import "TBXML.h"
#import "RssItem.h"
#import "GlobalCls.h"

@implementation GetRssDataLogical

@synthesize m_rsslist;
@synthesize m_strRssUrl;
@synthesize m_nPage;
@synthesize m_rssImglist;
@synthesize m_nRssImgPage;
@synthesize RssImgUrl = m_strRssImgUrl;

-(void) loadRssItems:(TBXMLElement *)root list:(NSMutableArray*) listAdd
{
	
	if (root) {
		TBXMLElement *rss = root;
		if (rss) {
			TBXMLElement *item = [TBXML childElementNamed:@"item" parentElement:rss];
			while (item != nil) {
				RssItem *rssAddItem = [[RssItem alloc] init];
				TBXMLElement *title = [TBXML childElementNamed:@"title" parentElement:item];
				if (title) {
					rssAddItem.title = [TBXML textForElement:title];
				}
				TBXMLElement *nID = [TBXML childElementNamed:@"id" parentElement:item];
				if (nID) {
					rssAddItem.n_id = [[NSURL alloc] initWithString:[TBXML textForElement:nID]];
				}
				TBXMLElement *imgLink = [TBXML childElementNamed:@"link" parentElement:item];
				if (imgLink) {
					rssAddItem.imglink = [[NSURL alloc] initWithString:[TBXML textForElement:imgLink]];
				}
				TBXMLElement *pubDate = [TBXML childElementNamed:@"idate" parentElement:item];
				if (pubDate) {
					rssAddItem.pubDate = [[NSURL alloc] initWithString:[TBXML textForElement:pubDate]];
				}
				[listAdd addObject:rssAddItem];
				item = [TBXML nextSiblingNamed:@"item" searchFromElement:item];
			}
			
		}
	}
	
}

- (id)init
{
	if ((self = [super init]) == nil)
		return nil;
	m_nPage = 1;
	return self;
}

- (void)createRssData {
	if (m_rsslist == nil) {
		m_rsslist = [[NSMutableArray alloc] init];
	}
	else {
		[m_rsslist removeAllObjects];
	}
	
	NSString* strRssUrl = [NSString stringWithFormat:@"%@?page=%d", m_strRssUrl, m_nPage ];
	
	NSString* strHtml = [GlobalCls getStringByCompressUrl:strRssUrl];
	TBXML * tbxml = [[TBXML alloc] initWithXMLString:strHtml];
	
//	NSURL *url = [[NSURL alloc] initWithString: strRssUrl];
//	TBXML * tbxml = [[TBXML alloc] initWithURL:url];
	
	TBXMLElement * root = tbxml.rootXMLElement;
	
	[self loadRssItems:root list:m_rsslist];
}

- (void)createRssImgData {
	if (m_rssImglist == nil) {
		m_rssImglist = [[NSMutableArray alloc] init];
	}
	else {
		[m_rssImglist removeAllObjects];
	}
	
	NSString* strRssUrl = [NSString stringWithFormat:@"%@?page=%d&refrash", m_strRssImgUrl, m_nPage ];
	
	NSString* strHtml = [GlobalCls getStringByCompressUrl:strRssUrl];
	TBXML * tbxml = [[TBXML alloc] initWithXMLString:strHtml];
	
	//	NSURL *url = [[NSURL alloc] initWithString: strRssUrl];
	//	TBXML * tbxml = [[TBXML alloc] initWithURL:url];
	
	TBXMLElement * root = tbxml.rootXMLElement;
	
	[self loadRssItems:root list:m_rssImglist];
}



@end
